package cn.tedu.mall.order.controller;

import cn.tedu.mall.common.restful.JsonPage;
import cn.tedu.mall.common.restful.JsonResult;
import cn.tedu.mall.order.service.IOmsCartService;
import cn.tedu.mall.pojo.order.dto.CartAddDTO;
import cn.tedu.mall.pojo.order.dto.CartUpdateDTO;
import cn.tedu.mall.pojo.order.vo.CartStandardVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/oms/cart")
@Api(tags = "购物车模块")
public class OmsCartController {

    @Autowired
    private IOmsCartService omsCartService;

    @PostMapping("/add")
    @ApiOperation("新增sku到购物车")
    @PreAuthorize("hasRole('user')")
    public JsonResult addCart(@Validated CartAddDTO cartAddDTO) {
        omsCartService.addCart(cartAddDTO);
        return JsonResult.ok("新增购物车完成");
    }

    @PostMapping("/delete")
    @ApiOperation("根据id的数组删除购物车中sku商品")
    @PreAuthorize("hasRole('user')")
    public JsonResult removeCartsById(Long[] ids) {
        omsCartService.removeCart(ids);
        return JsonResult.ok("删除成功");
    }

    @PostMapping("/delete/all")
    @ApiOperation("清空当前登陆用户的购物车")
    @PreAuthorize("hasRole('user')")
    public JsonResult removeCartsByUserId() {
        omsCartService.removeAllCarts();
        return JsonResult.ok("购物车清空");
    }

    //作业：查询购物车列表
    @GetMapping("/list")
    @ApiOperation("分页查询购物车")
    @PreAuthorize("hasRole('user')")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "page",name = "page",example = "1",dataType = "int"),
            @ApiImplicitParam(value = "pageSize",name = "pageSize",example = "2",dataType = "int"),
    })
    public JsonResult<JsonPage<CartStandardVO>> getCartsPage(Integer page, Integer pageSize){
        JsonPage<CartStandardVO> cartStandardVOJsonPage = omsCartService.listCarts(page, pageSize);
        return JsonResult.ok(cartStandardVOJsonPage);
    }

    @PostMapping("/update/quantity")
    @ApiOperation("修改购物车数量")
    @PreAuthorize("hasRole('user')")
    public JsonResult updateQuantity(@Validated CartUpdateDTO cartUpdateDTO){
        omsCartService.updateQuantity(cartUpdateDTO);
        return JsonResult.ok("修改成功");
    }
}
